package com.skirmisher.inspection.common.service.impl;

import com.skirmisher.inspection.common.entity.InsJobPlaceLogEntity;
import com.skirmisher.inspection.common.entity.InsPlaceLog;
import com.skirmisher.inspection.common.mapper.InsPlaceLogMapper;
import com.skirmisher.inspection.common.service.InsPlaceLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @ClassName InsPlaceLogServiceImpl
 * @Description TODO
 * @Author hanwangxin
 * @Date 2020/12/13 下午4:48
 * @Version 1.0
 */
@Slf4j
@Transactional
@Service
public class InsPlaceLogServiceImpl implements InsPlaceLogService {

    @Autowired
    private InsPlaceLogMapper insPlaceLogMapper;

    @Override
    public void updatePlaceLog(InsPlaceLog placeLog) {

        //封装处理其他参数
        placeLog.setId(UUID.randomUUID().toString());
        placeLog.setInTime(new Date());
        insPlaceLogMapper.insert(placeLog);


    }

    @Override
    public List<InsJobPlaceLogEntity> getPalceLog(String jobId) {
        //获取到该任务的所有轨迹
        List<InsPlaceLog> placeLogs = insPlaceLogMapper.getPalceLog(jobId);
        //根据人员进行分类
        Map<String,List<InsPlaceLog>> workerPlace = new HashMap<>();
        for (InsPlaceLog placeLog : placeLogs) {
            String workCode = placeLog.getWorkCode();
            if (workerPlace.containsKey(workCode)){
                List<InsPlaceLog> workerPlaceLogs = workerPlace.get(workCode);
                workerPlaceLogs.add(placeLog);
            }else {
                List<InsPlaceLog> workerPlaceLogs = new ArrayList<>();
                workerPlaceLogs.add(placeLog);
                workerPlace.put(workCode,workerPlaceLogs);
            }
        }
        //处理地点信息，将其置为前端需要的数据
        List<InsJobPlaceLogEntity> returnLogs = new ArrayList<>();
        for (String workCode : workerPlace.keySet()) {
            InsJobPlaceLogEntity workerLogs = new InsJobPlaceLogEntity();
            List<InsPlaceLog> nomLogs = workerPlace.get(workCode);
            workerLogs.setJobId(jobId);
            workerLogs.setWorkCode(nomLogs.get(0).getWorkCode());
            workerLogs.setWorkName(nomLogs.get(0).getWorkName());
            List<Double[]> logArray = new ArrayList<>();
            for (InsPlaceLog nomLog : nomLogs) {
                Double placeLongitude = nomLog.getPlaceLongitude();
                Double placeLatitude = nomLog.getPlaceLatitude();
                Double[] place = new Double[2];
                place[0] = placeLongitude;
                place[1] = placeLatitude;
                logArray.add(place);
            }
            workerLogs.setPlaceArray(logArray);
            returnLogs.add(workerLogs);
        }
        return returnLogs;
    }
}
